<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Partner;
use Illuminate\Http\Request;

class PartnerController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'partner';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-合伙人市场管理';
    }
    public function index(Request $request){
        $page = $request->get('page', 1);
        $search = $request->all();
        $this->_response['_active']['_action']      = 'list';

        $partnerModel = new Partner();
        $data = $partnerModel->getData($search, $page, 20);
        $this->_response['partner'] = $data;
        $this->_response['partner']['pageData']['page'] = $page;
        $this->_response['partner']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['partner']['pageData']['lastPage'] , '/admin/partner?');
        // return $this->_response;
        return view('admin.partner.list', $this->_response);
    }


    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.partner.add', $this->_response);
    }

    public function doAdd(Request $request){
        $data = $request->all();
        $saveData = [
            'nickname' => $data['nickname'],
            'avatar' =>Config('app.set.url').$data['img'],
            'inviter' => $data['inviter'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $partnerModel = new Partner();
        if($partnerModel->saveData($saveData)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    public function edit($id){
        $this->_response['partner'] = Partner::where('id', $id)->first();
        return view('admin.partner.edit', $this->_response);
    }

    public function doedit(\App\Http\Requests\Partner $request){
        $data = $request->all();
        print_r($data);
        $saveData = [
            'nickname' => $data['nickname'],
            'avatar' => $data['img'],
            'inviter' => $data['inviter'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $partnerModel = new Partner();
        if($partnerModel->setData($data['id'],$saveData)){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    public function delete($id){
        $partnerModel = new Partner();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($partnerModel->delData($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);

        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

}